using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DeliciousAPI;

namespace DeliciousToolbar
{
    public partial class BookmarkView : Form
    {
        PostCollection Bookmarks;
        private int TreeViews = 0;

        public string UrlSelected
        {
            get { return _UrlSelected; }
            private set { _UrlSelected = value; }
        } private string _UrlSelected = null;
        
        public BookmarkView()
        {
            InitializeComponent();
        }

        public BookmarkView(PostCollection posts)
        {
            this.Bookmarks = posts;
            InitializeComponent();
        }

        private void BookmarkView_Load(object sender, EventArgs e)
        {
            TreeNodeCollection TagNodes = this.AddTreeView().Nodes;

            List<TreeNode> UntaggedNodes = new List<TreeNode>();

            List<TreeNodeCollection> TreeviewNodes = new List<TreeNodeCollection>();
            TreeviewNodes.Add(TagNodes);

            int TagCount = 0;
            foreach(Post P in Bookmarks.Posts)
            {
                if (P.Tag != null && P.Tag.Trim().Length > 0 && P.Tag.Trim() != DeliciousAPI.PostConstants.__NO_TAG)
                {                  
                    string[] Tags = P.Tag.Trim().Split(' ');
                    foreach (string Tag in Tags)
                    {
                        bool TagFound = false;
                        foreach (TreeNodeCollection TreeTags in TreeviewNodes)
                        {
                            if (TreeTags.ContainsKey(Tag))
                            {
                                TreeNode PostNode = new TreeNode();
                                PostNode.Text = P.Description;
                                PostNode.Tag = P.Href;
                                PostNode.Name = P.Href;
                                PostNode.ToolTipText = P.Href;
                                TreeTags.Find(Tag, false)[0].Nodes.Add(PostNode);
                                TagFound = true;
                                break;
                            }
                        }

                        if(!TagFound)
                        {
                            if (++TagCount % 30 == 0)
                            {
                                TagNodes = this.AddTreeView().Nodes;
                                TreeviewNodes.Add(TagNodes);
                            }
                            TreeNode PostNode = new TreeNode();
                            PostNode.Text = P.Description;
                            PostNode.Tag = P.Href;
                            PostNode.Name = P.Href;
                            PostNode.ToolTipText = P.Href;

                            TreeNode TagNode = new TreeNode();
                            TagNode.Text = Tag;
                            TagNode.Tag = null;
                            TagNode.Name = Tag;
                            TagNode.Nodes.Add(PostNode); 
                            
                            TagNodes.Add(TagNode);
                        }
                    }
                }
                else
                {
                    TreeNode PostNode = new TreeNode();
                    PostNode.Text = P.Description;
                    PostNode.Tag = P.Href;
                    PostNode.Name = P.Href;
                    PostNode.ToolTipText = P.Href;
                    UntaggedNodes.Add(PostNode);
                }
            }
            //make sure untagged nodes are added last.
            TagNodes.AddRange(UntaggedNodes.ToArray());
        }

        private TreeView AddTreeView()
        {            
            TreeView BookmarkTree = new TreeView();
            BookmarkTree.BackColor = System.Drawing.SystemColors.MenuBar;
            BookmarkTree.BorderStyle = System.Windows.Forms.BorderStyle.None;
            BookmarkTree.Dock = System.Windows.Forms.DockStyle.None;
            BookmarkTree.Location = new System.Drawing.Point(this.TreeViews * 200, 0);
            BookmarkTree.Name = "BookmarkTree";
            BookmarkTree.Size = new System.Drawing.Size(200, 600);
            BookmarkTree.TabIndex = TreeViews;
            BookmarkTree.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.BookmarkTree_NodeMouseClick);

            this.Controls.Add(BookmarkTree);

            this.TreeViews++;

            return BookmarkTree;
        }

        #region Bookmark Clicked Event
        private void BookmarkTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Node.Tag != null)
            {
                this.UrlSelected = (string)e.Node.Tag;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
        #endregion
    }

}